package com.ming.rabbitmq.springbootrabbitmq.controller;

import com.ming.rabbitmq.springbootrabbitmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/ttl")
public class LonginController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendExpirationMsg/{message}")
    public void test(@PathVariable String message){

        log.info("当前时间：{}，发消息{}给两个队列",new Date().toString(),message);
        rabbitTemplate.convertAndSend("X","XA","消息来自10s的消息队列 "+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自40s的消息队列 "+message);
    }
    @GetMapping("/sendExpirationMsg/{message}/{ttl}")
    public void setMessage(@PathVariable String message,@PathVariable String ttl){


        rabbitTemplate.convertAndSend("X","XC",message,msg->{
            msg.getMessageProperties().setExpiration(ttl);
            return msg;
        });
        log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date(),ttl, message);

    }


    @GetMapping("/sendDelayMsg/{message}/{ttl}")
    public void setMsg(@PathVariable String message,@PathVariable Integer ttl){
        log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date().toString(),ttl, message);

        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE,
                DelayedQueueConfig.DELAYED_ROUTING_KEY,message, msg->{
            msg.getMessageProperties().setDelay(ttl);
            return msg;
        });

    }

}
